﻿using DAL.Comm;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class CustomerDAL
    {
        private SqlCommand cmd;//新建SqlCommand对象
        //int returnVal;//保存存储过程输出参数

        public CustomerDAL()
        {
            cmd = new SqlCommand();//实例化SqlCommand对象
            cmd.CommandType = CommandType.StoredProcedure;//定义SqlCommand对象访问数据库方式为存储过程
        }
        /// <summary>
        /// 针对数据库修改提出公共实现方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        private bool ForEdit(SqlCommand cmd)
        {
            try
            {
                int num = SQLHelper.ExecuteSQL(cmd);
                if (num >0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 加载存储过程参数方法实现
        /// </summary>
        /// <param name="bo">Category对象 bo</param>
        private void SetPara(Customer bo)
        {
            cmd.Parameters.Add("@CustomerID", SqlDbType.NVarChar, 6).Value    = bo.CustomerID;
            cmd.Parameters.Add("@CustomerName", SqlDbType.NVarChar, 80).Value = bo.CustomerName;
            cmd.Parameters.Add("@SpellingCode", SqlDbType.NVarChar, 20).Value = bo.ContactInfo.SpellingCode;
            cmd.Parameters.Add("@Address", SqlDbType.NVarChar, 80).Value      = bo.ContactInfo.Address;
            cmd.Parameters.Add("@ZipCode", SqlDbType.Char, 6).Value           = bo.ContactInfo.ZipCode;
            cmd.Parameters.Add("@Tel", SqlDbType.NVarChar, 20).Value          = bo.ContactInfo.Tel;
            cmd.Parameters.Add("@Fax", SqlDbType.NVarChar, 20).Value          = bo.ContactInfo.Fax;
            cmd.Parameters.Add("@BankName", SqlDbType.NVarChar, 40).Value     = bo.ContactInfo.BankName;
            cmd.Parameters.Add("@BankAccount", SqlDbType.NVarChar, 50).Value  = bo.ContactInfo.BankAccount;
            cmd.Parameters.Add("@Contacter", SqlDbType.NVarChar, 20).Value    = bo.ContactInfo.Contacter;
            cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 30).Value        = bo.ContactInfo.Email;
        }
        /// <summary>
        /// 新增数据方法实现
        /// </summary>
        /// <param name="bo">Category对象bo</param>
        /// <returns>布尔值</returns>
        public bool Insert(Customer bo)
        {
            cmd.CommandText = "InsertCustomer";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }
        /// <summary>
        /// 数据修改方法
        /// </summary>
        public bool Update(Customer bo)
        {
            cmd.CommandText = "UpdateCustomer";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }
        /// <summary>
        /// 数据删除方法
        /// </summary>
        public bool Delete(Customer bo)
        {
            cmd.CommandText = "DeleteCustomer";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@CustomerID", SqlDbType.NVarChar, 6).Value = bo.CustomerID;
            return ForEdit(cmd);
        }
        /// <summary>
        /// 将数据库读库结果返回到实体中
        /// </summary>
        /// <param name="read">读库结果</param>
        /// <returns>Customer业务实体bo</returns>
        private static Customer GetValueByDataRead(SqlDataReader read)
        {
            Customer bo = new Customer();
            bo.CustomerID               = read["CustomerID"].ToString().Trim();
            bo.CustomerName             = read["CustomerName"].ToString().Trim();

            bo.ContactInfo = new Contact();
            bo.ContactInfo.SpellingCode = read["SpellingCode"].ToString().Trim();
            bo.ContactInfo.Address      = read["Address"].ToString().Trim();
            bo.ContactInfo.ZipCode      = read["ZipCode"].ToString().Trim();
            bo.ContactInfo.Tel          = read["Tel"].ToString().Trim();
            bo.ContactInfo.Fax          = read["Fax"].ToString().Trim();
            bo.ContactInfo.BankName     = read["BankName"].ToString().Trim();
            bo.ContactInfo.BankAccount  = read["BankAccount"].ToString().Trim();
            bo.ContactInfo.Contacter    = read["Contacter"].ToString().Trim();
            bo.ContactInfo.Email        = read["Email"].ToString().Trim();
            return bo;
        }
        /// <summary>
        /// 根据传入的ID查找对应记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Customer FindByID(string id)
        {
            cmd.CommandText = "FindCustomerByID";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@CustomerID", SqlDbType.NVarChar, 6).Value = id;

            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
            Customer bo = new Customer();
            while (read.Read())
            {
                bo=GetValueByDataRead(read);//调用GetValueByDataRead方法将数据库读取到对象中
            }
            read.Close();//关闭读库
            return bo;
        }
        public List<Customer> GetList()
        {
            cmd.CommandText = "SelectCustomerList";//指定存储过程名称
            cmd.Parameters.Clear();//清空存储过程参数列表

            List<Customer> list = new List<Customer>();//定义泛型集合用于存储数据库返回数据

            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
            foreach(var item in read)
            {
                Customer c = GetValueByDataRead(read);
                list.Add(c);
            }
            read.Close();//关闭读库
            return list;
        }

    }
}
